﻿#include "handle_manage.h"

handle_manage::handle_manage()
{
    qRegisterMetaType<emit_type>("emit_type");
    connect(&clock,SIGNAL(timeout()),this,SLOT(Check_SQL()),Qt::QueuedConnection);
    connect(&check_sql,SIGNAL(timeout()),this,SLOT(Keep_SQL()),Qt::QueuedConnection);
    clock.start(5000);
    check_sql.start(300*1000);
}

handle_manage::~handle_manage()
{
    qDebug()<<"handle_manage析构";
    QMap  <Handle*,QThread*>::iterator i;
    for (i = thread_map.begin(); i != thread_map.end(); ++i) {    //对QMAP进行遍历
        i.key()->deleteLater();
        i.value()->quit();
        i.value()->wait();
        i.value()->deleteLater();
    }
    sql_handle->deleteLater();
    sql_thread->wait();
    sql_thread->deleteLater();;
}

void handle_manage::setHandleCount(int handleNum)
{
    this->handle_count=handleNum;
}

void handle_manage::init()
{
    for (int a=0;a<this->handle_count;a++){
        QThread   *thread=new QThread();
        Handle    *handle=new Handle(QString::number(a+1),false);
        thread_map[handle]=thread;     //设置线程map
        thread_status[handle]=true;
        thread_task_num[handle]=0;     //设置线程任务数
        connect(this,SIGNAL(Handlelocaldata(QString,QString,QString,local_socket*,Handle*,emit_type)),handle,SLOT(handle_localSocket(QString,QString,QString,local_socket*,Handle*,emit_type)),Qt::QueuedConnection);
        connect(this,SIGNAL(Handletcpdata(QString,QString,QString,tcp_socket*,Handle*,emit_type)),handle,SLOT(handle_tcpSocket(QString,QString,QString,tcp_socket*,Handle*,emit_type)),Qt::QueuedConnection);
        connect(handle,SIGNAL(l_SocketSendData(QString,QString,QString,local_socket*,emit_type,QString)),this,SLOT(Cl_SocketSendData(QString,QString,QString,local_socket*,emit_type,QString)),Qt::QueuedConnection);
        connect(handle,SIGNAL(t_SocketSendData(QString,QString,QString,tcp_socket*,emit_type,QString)),this,SLOT(Ct_SocketSendData(QString,QString,QString,tcp_socket*,emit_type,QString)),Qt::QueuedConnection);
        connect(handle,SIGNAL(finish_Task()),this,SLOT(Handle_free()),Qt::QueuedConnection);
        handle->moveToThread(thread);
        thread->start();
    }
    sql_thread=new QThread();
    sql_handle=new Handle("handle_sql",true);
    connect(this,SIGNAL(Handlelocaldata(QString,QString,QString,local_socket*,Handle*,emit_type)),sql_handle,SLOT(handle_localSocket(QString,QString,QString,local_socket*,Handle*,emit_type)),Qt::QueuedConnection);
    connect(this,SIGNAL(Handletcpdata(QString,QString,QString,tcp_socket*,Handle*,emit_type)),sql_handle,SLOT(handle_tcpSocket(QString,QString,QString,tcp_socket*,Handle*,emit_type)),Qt::QueuedConnection);
    connect(sql_handle,SIGNAL(l_SocketSendData(QString,QString,QString,local_socket*,emit_type,QString)),this,SLOT(Cl_SocketSendData(QString,QString,QString,local_socket*,emit_type,QString)),Qt::QueuedConnection);
    connect(sql_handle,SIGNAL(t_SocketSendData(QString,QString,QString,tcp_socket*,emit_type,QString)),this,SLOT(Ct_SocketSendData(QString,QString,QString,tcp_socket*,emit_type,QString)),Qt::QueuedConnection);
    connect(sql_handle,SIGNAL(finish_Task()),this,SLOT(Handle_free()),Qt::QueuedConnection);
    sql_handle->moveToThread(sql_thread);
    sql_thread->start();
    Handle  *handle= Choice_Free_Handle();
    MessageSocket socket;
    socket.Stype=Lsocket;
    Handle_Message("handle_manage","自动读取未完成任务","",socket,One);
    handle= Choice_Free_Handle();
    emit  Handlelocaldata("handle_manage","自动读取DLL信息","",NULL,handle,One);
}


//获取负载较少的处理线程
Handle *handle_manage::Choice_Free_Handle()
{
//    long long thread_min_cout=LONG_LONG_MAX;
//    Handle *handle=NULL;
    QMap  <Handle*,bool>::const_iterator i;
    for (i = thread_status.constBegin(); i != thread_status.constEnd(); ++i) {    //对QMAP进行遍历
        if(i.value()==true)
        {
            return i.key();
        }
    }
    return NULL;
}


void handle_manage::receive(QString id, QString type, QString data)
{
    //qDebug()<<id<<type;
    //    Handle *handle=Choice_Free_Handle();
    //    if (handle==NULL)
    //    {
    //        qDebug()<<"获取到的处理线程异常！！！";
    //        return ;
    //    }
    //    ++thread_task_num[handle];    //任务数+1
    if (local_socket* l_socket = dynamic_cast<local_socket*>(sender())){
        //   emit Handlelocaldata(id,type,data,l_socket,handle,One);
        MessageSocket   socket;
        socket.Stype=Lsocket;
        socket.l_socket=l_socket;
        Handle_Message(id,type,data,socket,One);
    }
    else if (tcp_socket *t_socket = dynamic_cast<tcp_socket*>(sender())){
        //        emit Handletcpdata(id,type,data,t_socket,handle,One);
        MessageSocket   socket;
        socket.Stype=Tsocket;
        socket.t_socket=t_socket;
        Handle_Message(id,type,data,socket,One);
    }
}

void handle_manage::Cl_SocketSendData(QString id, QString type, QString data, local_socket *l_socket, emit_type te_ype,QString socketType)
{
    emit this->l_SocketSendData(id,type,data,l_socket,te_ype,socketType);
}

void handle_manage::Ct_SocketSendData(QString id, QString type, QString data, tcp_socket *t_socket, emit_type te_ype, QString socketType)
{
    //  qDebug()<<type<<socketType;
    emit this->t_SocketSendData(id,type,data,t_socket,te_ype,socketType);
}


void handle_manage::Check_SQL()
{
   // Handle  *handle= Choice_Free_Handle();
    MessageSocket socket;
    socket.Stype=Lsocket;
    Handle_Message("handle_manage","定时清理SQL","",socket,One);
  //  emit  Handlelocaldata("handle_manage","定时清理SQL","",NULL,handle,One);
}

void handle_manage::Keep_SQL()
{
  //  emit  Handlelocaldata("handle_manage","SQL心跳连接","",NULL,NULL,All);
    MessageSocket socket;
    socket.Stype=Lsocket;
    Handle_Message("handle_manage","SQL心跳连接","",socket,One);
}

void handle_manage::Handle_Message(QString id, QString type, QString data, MessageSocket M_socket, emit_type te_ype)
{
    if(type=="定时清理SQL"||type=="自动读取未完成任务"||type=="查询任务"||type=="SQL心跳连接"||type=="传输任务文件"||type=="删除任务")
    {
        if(M_socket.Stype==Lsocket)
        {
            emit  Handlelocaldata(id,type,data,M_socket.l_socket,sql_handle,te_ype);
        }
        else
        {
            emit  Handletcpdata(id,type,data,M_socket.t_socket,sql_handle,te_ype);
        }
        return;
    }
    HandleMessage message(id,type,data,M_socket,te_ype);
    handle_message.append(message);
    distribution_Task();
}

void handle_manage::distribution_Task()
{
    if(handle_message.isEmpty())
    {
        return;
    }

    Handle *handle= Choice_Free_Handle();
    if(handle==NULL)
    {
        return;
    }
   HandleMessage message=handle_message.first();
   if(message.M_socket.Stype==Lsocket)
   {
       emit  Handlelocaldata(message.id,message.type,message.data,message.M_socket.l_socket,handle,message.te_ype);
   }
   else
   {
       emit  Handletcpdata(message.id,message.type,message.data,message.M_socket.t_socket,handle,message.te_ype);
   }
   thread_status[handle]=false;
   handle_message.removeFirst();
   distribution_Task();
}


void handle_manage::Handle_free()
{
   Handle *handle=dynamic_cast<Handle*>(sender());
   if(!thread_status.contains(handle))
   {
       return;
   }
   thread_status[handle]=true;
   distribution_Task();
}
